package SeparateAnalysis;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;

/**
 * Created by IntelliJ IDEA.
 * User: Mike
 * Date: 11/15/11
 * Time: 2:37 PM
 * To change this template use File | Settings | File Templates.
 */
public class ReadInData {
    private static ArrayList<Double> tempTCArr = new ArrayList<Double>();                    // don't have to read in all the data for the type of plotting I'm doing now
    //    public static ArrayList<Double> tempTFArr = new ArrayList<Double>();
    private static ArrayList<Double> tempACArr = new ArrayList<Double>();
    //    public static ArrayList<Double> tempAFArr = new ArrayList<Double>();
//    public static ArrayList<String> dateTime = new ArrayList<String>();
    public static ArrayList<Double> timeS = new ArrayList<Double>();
    private static ArrayList<Double> milliTimeArr = new ArrayList<Double>();
    private static ArrayList<Double> boilerTemp = new ArrayList<Double>();
    //    public static ArrayList<Double> distTemp = new ArrayList<Double>();
    private static ArrayList<Double> abv = new ArrayList<Double>();


    public static ArrayList<Double> vaporAbv = new ArrayList<Double>();
//    public static ArrayList<Integer> bottle = new ArrayList<Integer>();
    private static long endTime = 0;

    private String getTimeSpan() {
        Hashtable hashtable = new Hashtable();
        hashtable.put("5 min", "5"); //adding value to Hashtable
        hashtable.put("10 min", "10");
        hashtable.put("20 min", "20");
        hashtable.put("30 min", "30");
        hashtable.put("45 min", "45");
        hashtable.put("1 hr", "60");
        hashtable.put("2 hr", "120");
        hashtable.put("5 hr", "300");
        hashtable.put("10 hr", "600");
        hashtable.put("1 day", "1440");
        hashtable.put("2 days", "2880");
        hashtable.put("3 days", "4320");
        hashtable.put("4 days", "5760");
        hashtable.put("5 days", "7200");
        hashtable.put("6 days", "8640");
        hashtable.put("7 days", "10080");
        hashtable.put("8 days", "11520");
        hashtable.put("10 days", "14400");
        hashtable.put("30 days", "43200");
        String output = (String) hashtable.get(Data.timeSpan);
        return output;
    }

    public static ArrayList<Double> getVaporAbv() {
        return vaporAbv;
    }

    public ArrayList getColumnC() {
        return tempTCArr;
    }

    public ArrayList getAbv() {
        return abv;
    }

    public ArrayList getmilliTime() {
        return milliTimeArr;
    }

    private void convertTime(int milliTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
        Date resultdate = new Date(milliTime);
        System.out.println(sdf.format(resultdate));

    }

    public void Read() {
        BufferedReader readbuffer = null;
        String filePathName = "C:\\WhiskeyData\\output.txt";

        try {
            readbuffer = new BufferedReader(new FileReader(filePathName));      //	"" + DatFile.Path[DatFile.Index] + "\\" + DatFile.Name[DatFile.Index]));
        } catch (FileNotFoundException e1) {
            System.out.println("File not found:" + filePathName);
            e1.printStackTrace();
        }
        String strRead;
//		String FileData = currentDir; //not used...
        double startReadHere = 0.01;
        int i = 0;                         // where we are in the file
        int j = 0;                         // where we are in the data
        int k = 0;                         // index to import next set of data aquisition initialization
        int mult = 0;                     // num of restarts
        int multFlag = 0;                 //was there a restart in the last sequence?
        double addtime = 0.0;
        int m = 0;
        boolean UserTimeAdd = false;
        mult = multFlag;

        int sparse = 1;
        long totalMin = CountData();
        if (totalMin > Long.valueOf(getTimeSpan())) {
            startReadHere = (endTime - 60*1000*Long.valueOf(getTimeSpan()));
        }
        try {
            while ((strRead = readbuffer.readLine()) != null) {
                //System.out.println("i = " + i);
                String splitarray[] = strRead.split("\t");
                if (k == 0) {        // header
                    // datetime	millitime   min	columnC	columnF	ambientC	ambientF	bottleNumber	correctedAbv	uncorrectedAbv	distillateTempC	boilerTempC
                    //	0		1	        2		3		4			5			6				7				8				9				10
                    //                dateTime.clear();
                    milliTimeArr.clear();
                    //                timeS.clear();
                    tempTCArr.clear();
                    //                tempTFArr.clear();
                    tempACArr.clear();
                    vaporAbv.clear();
                    //                tempAFArr.clear();
                    //                bottle.clear();
                    abv.clear();
                    //                abvUncorr.clear();
                    //                distTemp.clear();
                    boilerTemp.clear();
                }
                if (k > 1) {
                    if (Double.parseDouble(splitarray[1].trim()) >= startReadHere) {        // read in data
                        if (splitarray.length == 13 && (sparse == 1 || (j % sparse) == 1)) {                         // check to make sure there is data there
                            //                        dateTime.add(splitarray[0].trim());
                            milliTimeArr.add(Double.parseDouble(splitarray[1].trim()));
                            //                        timeS.add(Double.parseDouble(splitarray[2].trim()));
                            tempTCArr.add(Double.parseDouble(splitarray[3].trim()));
                            //                        tempTFArr.add(Double.parseDouble(splitarray[4].trim()));
                            tempACArr.add(Double.parseDouble(splitarray[5].trim()));
                            //                        tempAFArr.add(Double.parseDouble(splitarray[6].trim()));
                            //                        bottle.add(Integer.parseInt(splitarray[7].trim()));
                            abv.add(Double.parseDouble(splitarray[8].trim()));
                            vaporAbv.add(Double.parseDouble(splitarray[9].trim()));
                            //                        distTemp.add(Double.parseDouble(splitarray[10].trim()));
                            boilerTemp.add(Double.parseDouble(splitarray[11].trim()));
                            j++;
                        }
                    }
                }
                k = k + 1;
                i = i + 1;
            }
            readbuffer.close();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


        addtime = 0;
    }

    private static long CountData() {
        System.out.println("Reading in DAT file....");                                    // import data from file (DatFile.Index), line by line (i), into arrays (j)
        BufferedReader readbuffer = null;
        String filePathName = "C:\\WhiskeyData\\output.txt";

        try {
            readbuffer = new BufferedReader(new FileReader(filePathName));
        } catch (FileNotFoundException e1) {
            System.out.println("File not found:" + filePathName);
            e1.printStackTrace();
        }
        String strRead;
//		String FileData = currentDir; //not used...
        int i = 0;                         // where we are in the file
        int j = 0;                         // where we are in the data
        int k = 0;                         // index to import next set of data aquisition initialization
        long initialMin = 0;
        long totalMin = 0;
        boolean firstLine = true;
        try {
            while ((strRead = readbuffer.readLine()) != null) {
                //System.out.println("i = " + i);
                String splitarray[] = strRead.split("\t");
                if (k == 0) {        // header
                    // datetime	millitime min	columnC	columnF	ambientC	ambientF	bottleNumber	correctedAbv	uncorrectedAbv	distillateTempC	boilerTempC
                    //	0		1	        2		3		4			5			6				7				8				9				10
                }
                if (k > 0) {        // read in data
                    if (splitarray.length >= 12) {                         // check to make sure there is data there
                        if (firstLine) {
                            double temp = Double.parseDouble(splitarray[1].trim());
                            initialMin = (long) temp;
                            firstLine = false;
                        }
                        double temp = Double.parseDouble(splitarray[1].trim());
                        totalMin = (long) temp;
                        j++;
                    }
                }
                k = k + 1;
                i = i + 1;
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            readbuffer.close();
        }                         //Close the stream to free memory resources on the host computer:
        catch (IOException e1) {
            e1.printStackTrace();
        }
        endTime = totalMin;
        System.out.println("totalMin readin = " + totalMin);
        totalMin = (totalMin - initialMin) / 1000 / 60;
        System.out.println("totalMin readin = " + totalMin);
        return totalMin;
    }

    private static void cleanUpArrays() {
//        dateTime.clear();
//        timeS.clear();
        tempTCArr.clear();
//        tempTFArr.clear();
        tempACArr.clear();
//        tempAFArr.clear();
//        bottle.clear();
        abv.clear();
//        abvUncorr.clear();
//        distTemp.clear();
        boilerTemp.clear();
    }

}
